
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
//Copyright 1997-2008 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
//
//    SWAF is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SWAF is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU Lesser General Public License for more details.

//    You should have received a copy of the GNU Lesser General Public License
//    along with SWAF.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;

using Swaf.BizObj;

namespace Swaf.Container
{
	[Serializable]
	public class DecimalFormattingNode : INameAddressSupport
	{
		protected decimal m_innerVal = 0M;

		public DecimalFormattingNode()
		{
		}

		public DecimalFormattingNode(decimal d)
		{
			m_innerVal = d;
		}

		public DecimalFormattingNode(DecimalFormattingNode c)
		{
			m_innerVal = c.m_innerVal;
		}

		public DecimalFormattingNode(string v)
		{
			put("", v);
		}
		#region INameAddressSupport Members

		public object get(string name)
		{
			if (name == null)
				return m_innerVal;
			if (name.Length == 0)
				return this;
			if (string.Compare(name, "cmpcur", true) == 0)
				name = "$CmpSetting(priceCurrencyPrefix)$#,###,##0.00";
			else if(string.Compare(name, "scmpcur", true) == 0)
				name = "$CmpSetting(priceCurrencyPrefix)$#,###,###.##";

			string userCulture = Application.currentApp.resMgr.CurrentCulture.get("dotNetBaseCulture").ToString();
			if (Cmn.IsEmpty(userCulture))
				userCulture = Application.currentApp.resMgr.BuiltInCulture.get("dotNetBaseCulture").ToString();
			if (userCulture == "ko")
				userCulture = "ko-KR";
			CultureInfo cinfo = new CultureInfo(userCulture, false);
			if (cinfo.IsNeutralCulture)
				cinfo = null;
			name = Application.globalApp.resMgr.resolve(name);
			return cinfo == null ? m_innerVal.ToString(name) : m_innerVal.ToString(name, cinfo);
		}

		public object get(string name, object oneArg)
		{
			return get(name);
		}

		public object get(string name, params object[] p)
		{
			return get(name);
		}

		public void put(string name, object newValue)
		{
			if (Cmn.IsEmpty(newValue))
				m_innerVal = 0M;
			else if (newValue is decimal)
				m_innerVal = (decimal)newValue;
			else
			{
				string f = newValue.ToString();
				f = Regex.Replace(f, @"[^\d|\.|-]", "");
				m_innerVal = decimal.Parse(f);
			}
		}

		public void put(string nameAddress, object val, object extraInfo)
		{
			put(nameAddress, val);
		}

		public bool isGetOnly
		{
			get { return false; }
		}

		public XmlSerializationSupport SerializationSupport
		{
			get { return XmlSerializationSupport.AsAttribute; }
		}

		public string Xml
		{
			get { return ""; }
		}

		public string getXml(string options)
		{
			return Xml;
		}

		public bool containsField(string nameAddress)
		{
			return true;
		}

		#endregion

		#region IEnumerable Members

		public IEnumerator GetEnumerator()
		{
			throw new Exception("The method or operation is not implemented.");
		}

		#endregion
	}

	[Serializable]
	public class DateFormattingNode : INameAddressSupport
	{
		protected DateTime m_innerVal;

		public DateFormattingNode()
		{
		}

		public DateFormattingNode(DateTime d)
		{
			m_innerVal = d;
		}

		public DateFormattingNode(DateFormattingNode c)
		{
			m_innerVal = c.m_innerVal;
		}

		public DateFormattingNode(string v)
		{
			put("", v);
		}
		#region INameAddressSupport Members

		public static string FormatDate(DateTime date, string format, bool useUserTimeZone)
		{
            TimeZoneInfo tz = null;
            if (useUserTimeZone && Application.currentApp.securityMgr.currentPrincipal.additionalInfo != null &&
                Application.currentApp.securityMgr.currentPrincipal.additionalInfo.containsField("settings.defaultTimeZone"))
            {
                IBizObj timeZone = Application.currentApp.securityMgr.currentPrincipal.additionalInfo.get("settings.defaultTimeZone") as IBizObj;
                if (timeZone != null && !Cmn.IsEmpty(timeZone.get("id")))
                {
                    //hrs = (int)timeZone.get("hoursFromUTC");
                    //date = date.AddHours(hrs);
                    tz = TimeZoneInfo.FindSystemTimeZoneById((string)timeZone.get("id"));
                    date = TimeZoneInfo.ConvertTimeFromUtc(date, tz);
                }
            }
			if (Cmn.IsEmpty(format))
				return date.ToString();

            DateTime n = DateTime.UtcNow;
            if (tz != null)
                n = TimeZoneInfo.ConvertTimeFromUtc(n, tz);
			bool easyDateOnly = string.Compare(format, "easyDateOnly", true) == 0;
			if (string.Compare(format, "easy", true) == 0 || easyDateOnly)
			{
				if (date.Year == n.Year && date.Month == n.Month && date.Day == n.Day)
					format = "today, " + (easyDateOnly?"":date.ToString("h:mm tt"));
				else
				{
					n = n.AddDays(-1);
					if (date.Year == n.Year && date.Month == n.Month && date.Day == n.Day)
						format = "yesterday, " + (easyDateOnly ? "" : date.ToString("h:mm tt"));
					else if (easyDateOnly)
						format = date.ToString("MMM d, yy");
					else
						format = date.ToString("MMM d, yy -h:mm tt");
				}
				return format;
			}

			format = Application.globalApp.resMgr.resolve(format);
			return date.ToString(format);
		}

		public object get(string name)
		{
			if (name == null)
				return m_innerVal;
			if (name.Length == 0)
				return this;
			return FormatDate(m_innerVal, name, true);
		}

		public object get(string name, object oneArg)
		{
			return get(name);
		}

		public object get(string name, params object[] p)
		{
			return get(name);
		}

		public void put(string name, object newValue)
		{
			if (Cmn.IsEmpty(newValue))
				m_innerVal = DateTime.Now;
			else if (newValue is DateTime)
				m_innerVal = (DateTime)newValue;
			else
			{
				string f = newValue.ToString();
				m_innerVal = DateTime.Parse(f);
			}
		}

		public void put(string nameAddress, object val, object extraInfo)
		{
			put(nameAddress, val);
		}

		public bool isGetOnly
		{
			get { return false; }
		}

		public XmlSerializationSupport SerializationSupport
		{
			get { return XmlSerializationSupport.AsAttribute; }
		}

		public string Xml
		{
			get { return ""; }
		}

		public string getXml(string options)
		{
			return Xml;
		}

		public bool containsField(string nameAddress)
		{
			return true;
		}

		#endregion

		#region IEnumerable Members

		public IEnumerator GetEnumerator()
		{
			throw new Exception("The method or operation is not implemented.");
		}

		#endregion
	}
}
